Apache CXF Web Service开发笔记 - 01

Apache CXF Web Service开发笔记 - 01

Naveen Balani, Rajeev Hathi. Apache CXF Web Service Development - Develop and deploy SOAP and RESTful Web Services. 2009 Packt Publishing

熟悉CXF

Apache CXF 介绍

https://www.ibm.com/developerworks/cn/education/java/j-cxf/

Apache CXF 是一个开源的 Services 框架,CXF 帮助您利用 Frontend 编程 API 来构建和开发 Services ,像 JAX-WS 。这些 Services 可以支持多种协议,比如:SOAP、XML/HTTP、RESTful HTTP 或者 CORBA ,并且可以在多种传输协议上运行,比如:HTTP、JMS 或者 JBI,CXF 大大简化了 Services 的创建,同时它继承了 XFire 传统,一样可以天然地和 Spring 进行无缝集成。

CXF 继承了 Celtix 和 XFire 两大开源项目的精华,提供了对 JAX-WS 全面的支持,并且提供了多种 Binding 、DataBinding、Transport 以及各种 Format 的支持,并且可以根据实际项目的需要,采用代码优先(Code First)或者 WSDL 优先(WSDL First)来轻松地实现 Web Services 的发布和使用。

相关术语

  • JAX-WS——Java API for XML Web Services
  • WSDL——Web Services Description Language 网络服务描述语言
  • MTOM——Message Transmission Optimization Mechanism
  • JAXB——Java Architecture for XML Binding
  • SAAJ——SOAP with Attachments API for Java
  • WS-I——Web Services Interoperability industry consortium

Web service 标准技术

XML (可扩展标记语言,Extensible Markup Language)

XML规范或描述了用于交换的数据的格式。这些数据采用标签或元素以一种层次结构实现结构化表达。

XML is a markup language that specifies or describes the format of the data to be exchanged between two parties. The data is significantly structured as tags or elements in a hierarchical order.

与XML文档相关的标准技术:

  • XML namespace —— XML命名空间是XML文档提供唯一的命名元素和属性的标准。它与Java中的包(package)概念类似,用于解决不同类之间的命名冲突。XML namespace 用xmlns属性声明,其值必须是一个URI(Uniform Resource Identifier)

  • XML schema —— XML模式提供了定义XML文档结构、内容和语义的一组方法。XML Schema 数据模型包括词汇 vocabulary (元素和属性名 element and attribute names), 内容模型 (关系与结构relationships and structure), 和数据类型.

    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="address" type="Address"/>
    <xs:complexType name="Address">
    <xs:sequence>
    <xs:element name="addressLine1" type="xs:string"/>
    <xs:element name="addressLine2" type="xs:string"/>
    <xs:element name="city" type="xs:string"/>
    <xs:element name="state" type="xs:string"/>
    <xs:element name="country" type="xs:string"/>
    </xs:sequence>
    </xs:complexType>
    </xs:schema>

    上述例子中,xs 表示 XML Schema的命名空间. address表示一个类型为Address的元素,而Address是一个complexType(类似于Java中的class),其由string类型的addressLine1,addressLine2, citystate,和”country”组成。

SOAP (简单对象访问协议,Simple Object Access Protocol)

SOAP是基于网络(主要是HTTP协议)交换XML信息的一种协议。SOAP消息格式由一个SOAP Envelope(封套、封装)包裹所有请求信息组成。SOAP Envelope又由可选的报头(消息头)和报体(header、body)组成。报头中可以包括诸如身份认证、事务、路由等方面的信息。Body 元素中包含发送给最终目标节点的信息、数据。

<?xml version="1.0"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://apress.com/beginjava6/address"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soapenv:Header></soapenv:Header>
<soapenv:Body>
<ns1:Address>
<ns1:addressLine1>1501ACity</ns1:addressLine1>
<ns1:addressLine2>UCity</ns1:addressLine2>
<ns1:city>SFO</ns1:city>
<ns1:state>CA</ns1:state>
<ns1:country>US</ns1:country>
</ns1:Address>
</soapenv:Body>
</soapenv:Envelope>

WSDL (网络服务描述语言,Web Services Description language)

最好是参考 http://www.w3school.com.cn/wsdl/index.asp

WSDL是基于标准XML来描述Web Service的语言。WSDL中,WebServices被描述为一组能进行消息交换的交流端点(endpoint),这些处理消息的网络端点被称为端口(port)。一个端点包含两个部分:

  • 由服务和消息(输入输出参数类型)提供的用于激活服务的操作的抽象定义(abstract definitions of operations)。一组抽象定义被当做端口类型

    the abstract definitions of operations (similar to methods in Java) provided by the services and messages (input and output parameter types for methods) which are needed to invoke the service. The set of abstract operation definitions is referred to as port type.

  • 抽象定义与具体网络协议的具体绑定。

    the concrete binding of those abstract definitions of operations to concrete network protocol, where the service is located, and message format for the service.

WSDL绑定描述了服务是如何与消息协议,特别是SOAP协议绑定的。WSDL文档通常由WebService框架提供的工具生成。

REST (表述性状态转移,Representational State Transfer)

REST是一种架构风格——在网络上表述资源的一组指导(a set of guidelines for exposing resources over the Web),而非技术或标准。REST架构风格与用URI表达的资源相关。具体参考:理解RESTful架构

服务注册 Service Registry

Service Registry 提供了一种查找web 服务的机制。传统上,一般使用UDDI 规范来实现服务注册和发现,但是其没有被相关企业大量采用。各个企业采用了自己的一套服务注册版本。

web services介绍

W3C对Web Service的定义:

A Web service is a software system identified by a URI whose public interfaces and bindings are defined and described using XML (specifically WSDL). Its definition can be discovered by other software systems. These systems may then interact with the web service in a manner prescribed by its definition, using XML-based messages conveyed by Internet protocols.

  • Web Services 是应用程序组件
  • Web Services 使用开放协议进行通信
  • Web Services 是独立的(self-contained)并可自我描述
  • Web Services 可通过使用UDDI来发现
  • Web Services 可被其他应用程序使用
  • XML 是 Web Services 的基础

Web Service 可以被看做是一个可被发布、定位、通过网络激活的独立的、可自我描述的、模块化的应用程序。Web services最大的好处是互操作性。

web service包含三种类型角色: service consumer, service provider, 可选的 service registry。

在基于SOAP的web service中,服务提供者通过网络或服务注册发布契约(contract),也即WSDL文件。消费者根据WSDL文件利用框架提供的工具生成客户端代码来与服务端交互。

而在RESTful风格的web service中,service consumer和service provider之间不会有正式的契约(contract),服务请求者需要知道消息的类型,如XML或JSON,以及支持的操作。服务提供者通过标准HTTP方法,如GET或POST方法来暴露相关操作,然后请求者利用资源的URI通过HTTP协议来激活相应的方法获取资源。

Web service SOAP 通信风格(communication styles)

web service SOAP communication style在服务提供者和服务消费者之间的XML消息通信中扮演着重要的角色。

目前有两种SOAP消息风格(SOAP message styles)类型:DocumentRPC

SOAP message styles作为SOAP绑定在WSDL文档中定义。SOAP绑定可以编码或文本方式使用。

Document 风格,将通常用XML schema定义生成的具有良好约定(contracts)的XML文档作为有效载荷数据( payloads)。为了获得更好的互操作性,文本文档风格是首选方式。

RPC (远程过程调用,Remote Procedure Call) 风格预示着SOAP消息体(body)包含一个方法的XML表达。

JAX-WS(Java API for XML Web Services)

JAXB(Java Architecture for XML Binding)

通过提供一种将XML模式映射到Java代码表达的便利方式,JAXB具备了数据绑定能力。

JAXB offers data binding capabilities by providing a convenient way to map XML schema to a epresentation in Java code.

文章目录
  1. 1. Apache CXF Web Service开发笔记 - 01
    1. 1.1. 熟悉CXF
      1. 1.1.1. Apache CXF 介绍
    2. 1.2. 相关术语
      1. 1.2.1. Web service 标准技术
        1. 1.2.1.1. XML (可扩展标记语言,Extensible Markup Language)
        2. 1.2.1.2. SOAP (简单对象访问协议,Simple Object Access Protocol)
        3. 1.2.1.3. WSDL (网络服务描述语言,Web Services Description language)
        4. 1.2.1.4. REST (表述性状态转移,Representational State Transfer)
        5. 1.2.1.5. 服务注册 Service Registry
        6. 1.2.1.6. web services介绍
        7. 1.2.1.7. Web service SOAP 通信风格(communication styles)
        8. 1.2.1.8. JAX-WS(Java API for XML Web Services)
        9. 1.2.1.9. JAXB(Java Architecture for XML Binding)
|